from django.shortcuts import render
from todoList.models import item
from django.http import HttpResponse
import json


# Create your views here.
class SuccessResponse(HttpResponse):

    def __init__(self, data, msg="成功"):
        res = {
            'code': 200,
            'message': msg,
            'data': data
        }
        super().__init__(json.dumps(res, ensure_ascii=False))


class ErrorResponse(HttpResponse):
    pass


def add_todo(request):
    res = {
        'code': 500,
        'message': '未知原因出错',
        'data': {}
    }
    if request.method == 'POST':
        a = json.loads(request.body)
        todo = item(id=a['id'], title=a['title'], iscomplete=False)
        todo.save()
        res['code'] = 200
        res['message'] = '添加数据成功'
    return HttpResponse(json.dumps(res, ensure_ascii=False))


def get_todo(request):
    res = {
        'code': 500,
        'message': '未知原因出错',
        'data': {}
    }
    todoList = list(item.objects.values())
    res['code'] = 200
    res['data']['todoList'] = todoList
    res['data']['total'] = len(todoList)
    res['message'] = '查询所有数据'
    return HttpResponse(json.dumps(res, ensure_ascii=False))


def change_state(request):
    res = {
        'code': 500,
        'message': '未知原因出错',
        'data': {}
    }
    if request.method == 'POST':
        a = json.loads(request.body)
        todo = item.objects.get(id=a['id'])
        todo.iscomplete = False if todo.iscomplete else True
        todo.save()
        res['code'] = 200
        res['message'] = '完成状态修改成功'
    return HttpResponse(json.dumps(res, ensure_ascii=False))


def delete_all_changed_todo(request):
    res = {
        'code': 500,
        'message': '未知原因出错',
        'data': {}
    }
    if request.method == 'POST':
        item.objects.filter(iscomplete=True).delete()
        res['code'] = 200
        res['message'] = '已删除已完成项目'
    return HttpResponse(json.dumps(res, ensure_ascii=False))


def delete_todo_byId(request):
    res = {
        'code': 500,
        'message': '未知原因出错',
        'data': {}
    }
    if request.method == 'POST':
        a = json.loads(request.body)
        item.objects.get(id=a['id']).delete()
        res['code'] = 200
        res['message'] = '项目删除成功'
    return HttpResponse(json.dumps(res, ensure_ascii=False))


def changeAllState(request):
    res = {
        'code': 500,
        'message': '未知原因出错',
        'data': {}
    }
    if request.method == 'POST':
        arr = list(item.objects.filter(iscomplete=False).values())
        if len(arr):
            item.objects.filter(iscomplete=False).update(iscomplete=True)
        else:
            item.objects.filter(iscomplete=True).update(iscomplete=False)
        # item.save()
        res['code'] = 200
        res['message'] = '所有状态已经更新完成'
    return HttpResponse(json.dumps(res, ensure_ascii=False))


def getState(request):
    res = {
        'code': 500,
        'message': '未知原因出错',
        'data': {}
    }
    if request.method == 'GET':
        arr = list(item.objects.filter(iscomplete=False).values())
        if len(arr):
            res['data']['state'] = False
        else:
            res['data']['state'] = True
        res['data']['count'] = len(list(item.objects.all()))
        res['code'] = 200
        res['message'] = '获取最新状态'
    return HttpResponse(json.dumps(res, ensure_ascii=False))
